package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.common.utils.MyPageHelper;
import com.project.common.utils.Util;
import com.project.entity.Image;
import com.project.entity.OrgInfo;
import com.project.myBeans.R;
import com.project.myBeans.ResponseBean;
import com.project.service.ImagesService;
import com.project.service.OrgInfoService;
import com.project.service.base.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/27 15:32
 * @param:
 * @return:
 */
@EnableAutoConfiguration //自动载入应用程序所需的所有Bean
@RestController //@Controller与@ResponseBody的合并
@RequestMapping(value = "/orginfo")
@Api(description = "公司信息",value = "orginfo")
public class OrginfoController extends BaseService {
    @Autowired
    private OrgInfoService orgInfoService;
    @Autowired
    private ImagesService imagesService;
    /**
     * TateBrown 2018年5月27日
     *
     * @param orgInfo
     * @return
     */
    @ApiOperation("模糊搜索分页显示")
    @PostMapping("/findByCondition")
    public R findByCondition(@RequestBody OrgInfo orgInfo) {
        try {
            List<OrgInfo> orgInfoList;
            orgInfo.setPageIndex(orgInfo.getPageIndex() - 1);
            orgInfoList = orgInfoService.findByCondition(orgInfo);
            return R.ok("查询成功").put("data",orgInfoList).put("total", MyPageHelper.getTotal(orgInfoList));
        } catch (Exception e) {
            return R.error("查询失败");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param managerId
     * @return
     */
    @ApiOperation("根据managerId查找公司信息")
    @GetMapping("/findByManagerId/{managerId}")
    public R findByManagerId(@PathVariable("managerId") Integer managerId) {
        try {
            OrgInfo orgInfo = orgInfoService.findByManagerId(managerId);
            if (orgInfo == null) {
               return R.ok("无对应公司");
            } else {
                Image businessliscense = null;
                Image outsidepic = null;
                JSONObject jsonObject = new JSONObject();
                if (orgInfo.getBusinessliscense() != null) {
                    businessliscense = imagesService.findById(orgInfo.getBusinessliscense());
                    String rootPath = Util.getWebRootURL(this);
                    // 解密营业执照
                    String absolutePath = rootPath + configParameters.getBusinessSubPath() + businessliscense.getPath();
                    String base64 = imagesService.dec(absolutePath);//base64编码的图片数据
                    businessliscense.setPath(base64);
                }
                if (orgInfo.getOutsidepic() != null) {
                    outsidepic = imagesService.findById(orgInfo.getOutsidepic());
                    outsidepic.setPath(configParameters.getServer() + configParameters.getOutSubPath() + outsidepic.getPath());
                }
                jsonObject.put("orgInfo", orgInfo);
                jsonObject.put("businessliscense", businessliscense);
                jsonObject.put("outsidepic", outsidepic);

                return R.ok("获取成功").put("data",jsonObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }

    }

    /**
     * TateBrown 2018年5月27日
     *
     * @param id
     * @return
     */
    @ApiOperation("通过id查找公司信息")
    @GetMapping("/findById/{id}")
    public R findById(@PathVariable("id") Integer id, HttpServletRequest request) {
        try {
            OrgInfo orgInfo = orgInfoService.findByid(id);
            if (orgInfo == null) {
                return R.ok("无对应公司");
            } else {
                Image businessliscense = null;
                Image outsidepic = null;
                JSONObject jsonObject = new JSONObject();
                if (orgInfo.getBusinessliscense() != null) {
                    businessliscense = imagesService.findById(orgInfo.getBusinessliscense());
                    String rootPath = Util.getWebRootURL(this);
                    // 解密营业执照
                    String absolutePath = rootPath + configParameters.getBusinessSubPath() + businessliscense.getPath();
                    String base64 = imagesService.dec(absolutePath);//base64编码的图片数据
                    businessliscense.setPath(base64);
                }
                if (orgInfo.getOutsidepic() != null) {
                    outsidepic = imagesService.findById(orgInfo.getOutsidepic());
                    outsidepic.setPath(configParameters.getServer() + configParameters.getOutSubPath() + outsidepic.getPath());
                }
                jsonObject.put("orgInfo", orgInfo);
                jsonObject.put("businessliscense", businessliscense);
                jsonObject.put("outsidepic", outsidepic);
                return R.ok("查询成功").put("data",jsonObject);
            }
        } catch (Exception e) {
            return R.error("程序异常");
        }
    }

    /**
     * TateBrown 2018年5月27日
     *
     * @param orgInfo
     * @return
     */
    @ApiOperation("（根据managerId）新建或修改公司信息")
    @PostMapping("/modifyOrAddByManagerId")
    public R modifyByManagerId(@RequestBody OrgInfo orgInfo) {
        try {
            if (orgInfo.getManagerid() != null) { //先查找改管理员是否有对应公司
                OrgInfo orgInfo1 = orgInfoService.findByManagerId(orgInfo.getManagerid());
                if (orgInfo1 != null) {//有则更新 无则插入
                    if(processSuccessDeleteUpdate(orgInfoService.modifyByManagerId(orgInfo)))
                        return R.ok("修改成功");
                    else
                        return R.error("未修改");
                }
                else {
                    if(processSuccessDeleteUpdate(orgInfoService.addOrgInfo(orgInfo)))
                        return R.ok("增加成功");
                    else
                        return R.error("增加失败");
                }
            } else {
                if(processSuccessDeleteUpdate(orgInfoService.addOrgInfo(orgInfo)))
                    return R.ok("增加成功");
                else
                    return R.error("增加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常，保存失败");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param id 公司信息id
     * @param state {int}状态 1- 待审核 2- 审核通过 3- 审核不通过
     * @return
     */
    @ApiOperation("根据id修改公司状态")
    @GetMapping("/modifyState/{id}/{state}")
    public R modifyState(@PathVariable("id") Integer id, @PathVariable("state") Integer state) {
        ResponseBean responseBean = new ResponseBean();
        try {
            OrgInfo orgInfo = orgInfoService.findByid(id);
            if (orgInfo != null) {
                orgInfo.setState(state);
                if(processSuccessDeleteUpdate(orgInfoService.modifyById(orgInfo)))
                    return R.ok("修改成功");
                else
                    return R.error("修改失败");
            } else {
                return R.error("公司不存在");
            }
        } catch (Exception e) {
            return  R.error("程序异常");
        }

    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param orgInfo
     * @return
     */
    @ApiOperation("添加一个新的公司信息")
    @PostMapping("/addOne")
    public R addOne(@RequestBody OrgInfo orgInfo) {
        try {
            orgInfoService.addOrgInfo(orgInfo);
            return R.ok("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param orgInfo
     * @return
     */
    @ApiOperation("根据id修改公司信息")
    @PostMapping("/modifyOneById")
    public R modifyOneById(@RequestBody OrgInfo orgInfo) {
        try {
            orgInfoService.modifyById(orgInfo);
            return R.ok("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param
     * @return
     */
    @ApiOperation("获取所有")
    @GetMapping("/findAll")
    public R findAll() {

        try {
            List<OrgInfo> orgInfoList = orgInfoService.findAll();
            return R.ok("修改成功").put("data",orgInfoList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
}
